﻿Imports ATS.EncryptDecryptKeys
Public Class ATSValidation

    
    Public Shared Function LicenseStatus() As vLicenseStatus
        Dim kstrRegSubKeyName As String = "GENCPROS\\ATS2.1" 'The name for the sub-key to store registry info
        Dim txtPassPhrase As String = "JNGforGENCPROS"
        Dim licenseDaysRemaining As Integer
        Dim licStatus As New vLicenseStatus

        Dim oReg As Microsoft.Win32.RegistryKey
        oReg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", True)
        oReg = oReg.CreateSubKey(kstrRegSubKeyName)
        oReg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\" & kstrRegSubKeyName)
        Dim strOldDay As String = oReg.GetValue("ActiveConnectedKeys", "").ToString
        Dim strOldMonth As String = oReg.GetValue("ActiveConnectedModeKeys", "").ToString
        Dim strOldYear As String = oReg.GetValue("ActiveUniqueKeys", "").ToString

        Dim licenseKey As String = oReg.GetValue("LicenseKey", "").ToString
        Dim productKey As String = oReg.GetValue("ProductKey", "").ToString
        Dim strTrialDone As String = oReg.GetValue("Enable", "").ToString
        oReg.Close()

        'If the keys are encrypted, decrypt them.
        'If EncryptKeys = True Then
        strOldDay = CommonMethods.Decrypt(txtPassPhrase, strOldDay)
        strOldMonth = CommonMethods.Decrypt(txtPassPhrase, strOldMonth)
        strOldYear = CommonMethods.Decrypt(txtPassPhrase, strOldYear)
        'End If

        If licenseKey = String.Empty Then
            licStatus.isValidLicense = False
            licStatus.licenseStatus = "License key not found. Please run ATS License Manager to register your license on this machine."
            Return licStatus
        End If
        'Define global variables.
        licenseDaysRemaining = DiffDate(strOldDay, strOldMonth, strOldYear)

        'Fill the progress bar
        licStatus.licenseStatus = DisplayApplicationStatus(licenseDaysRemaining, 0) 'DisplayApplicationStatus(DiffDate(strOldDay, strOldMonth, strOldYear), mintTrialPeriod)

        'Disable the continue button if the trial is over
        If DiffDate(strOldDay, strOldMonth, strOldYear) <= 0 Then
            'unregbutton.Enabled = False
            licStatus.isValidLicense = False
            oReg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", True)
            oReg = oReg.CreateSubKey(kstrRegSubKeyName)
            oReg.SetValue("Enable", "1")
            oReg.Close()
        End If

        'If the date is earlier than possible, then disable the program.
        If strOldMonth = "" Then
        Else
            Dim dtmOldDate As Date = New Date(Convert.ToInt32(strOldYear), Convert.ToInt32(strOldMonth), Convert.ToInt32(strOldDay))
            If Date.Compare(dtmOldDate, DateTime.Now) < 0 Then
                'unregbutton.Enabled = False
                licStatus.isValidLicense = False
                oReg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", True)
                oReg = oReg.CreateSubKey(kstrRegSubKeyName)
                oReg.SetValue("Enable", "1")
                oReg.Close()
            End If
        End If


        'If the trial is done then disable the button
        If strTrialDone = "1" Then
            'unregbutton.Enabled = False
            licStatus.isValidLicense = False
            licStatus.licenseStatus = "The system clock has been manually changed, and the application has been locked out to prevent unauthorized access!"
        End If

        ''See if the user is already registered, if so re-process the info and check if the computer is all okay.
        'If strRegName = "" Then
        'Else
        '    Dim strRN As String = Decrypt(txtPassPhrase.Text, strRegName)
        '    Dim strRC As String = Decrypt(txtPassPhrase.Text, strRegCode)
        '    Dim UserName As String = strRegName
        '    UserName = UserName.Remove(16, (UserName.Length - 16))
        '    If UserName = Decrypt(txtPassPhrase.Text, strRegCode) Then
        '        If Encrypt(txtPassPhrase.Text, cHardware.GetMotherBoardID.Trim.ToString) = strCompID Then
        '            mblnInTrial = False
        '            mblnFullVersion = True

        '            strRC = strRC.Insert(4, "-")
        '            strRC = strRC.Insert(8, "-")
        '            strRC = strRC.Insert(12, "-") 'Add dashes to make it look cool

        '            lblApplicationStatus.Text = "Licensed version to " + strRN + " with the key " + strRC
        '            oReg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", True)
        '            oReg = oReg.CreateSubKey(kstrRegSubKeyName)
        '            oReg.SetValue("Enable", "")
        '            oReg.Close()
        '        End If

        '    End If
        'End If
        Return licStatus
    End Function

#Region " -- License Status -- "
    Private Shared Function DiffDate(ByVal OrigDay As String, ByVal OrigMonth As String, ByVal OrigYear As String) As Integer
        Try
            Dim D1 As Date = New Date(Convert.ToInt32(OrigYear), Convert.ToInt32(OrigMonth), Convert.ToInt32(OrigDay))
            Return Convert.ToInt32(DateDiff(DateInterval.Day, DateTime.Now, D1))
        Catch
            Return 0
        End Try
    End Function

    Private Shared Function DisplayApplicationStatus(ByVal pDaysUsed As Integer, ByVal pTotalDays As Integer) As String
        'If pDaysUsed < 0 Then
        '    Return "An error has occurred, license days lower than 0."
        'End If

        'Check if the trial is expired
        If pDaysUsed < 0 Then
            Return "Your ATS license has expired!"
        End If

        'Draw the bar
        Return "You have " + pDaysUsed.ToString + " days remaining in your current license period."
    End Function

#End Region ' -- License Status -- 
End Class


Public Structure vLicenseStatus
    Dim isValidLicense As Boolean
    Dim licenseStatus As String
End Structure